package com.ruoyi.rocky.service.impl;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.rocky.mapper.PcProductMapper;
import com.ruoyi.rocky.mapper.PcStoreJoinMapper;
import com.ruoyi.rocky.mapper.PcStoreMapper;
import com.ruoyi.rocky.domain.PcExamine;
import com.ruoyi.rocky.domain.PcStore;
import com.ruoyi.rocky.domain.PcStoreJoin;
import com.ruoyi.rocky.service.PcStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;

@Service
public class PcStoreServiceImpl implements PcStoreService {

    @Autowired
    private PcStoreMapper pcStoreMapper;

    @Autowired
    private PcStoreJoinMapper pcStoreJoinMapper;

    @Autowired
    private PcProductMapper pcProductMapper;

    /**
     * 查询店铺
     *
     * @param id 店铺主键
     * @return 店铺
     */
    @Override
    public PcStore selectPcStoreById(Long id) {
        return pcStoreMapper.selectPcStoreById(id);
    }

    /**
     * 查询店铺列表
     *
     * @param pcStore 店铺
     * @return 店铺
     */
    @Override
    public List<PcStore> selectPcStoreList(PcStore pcStore) {
        return pcStoreMapper.selectPcStoreList(pcStore);
    }

    /**
     * 申请店铺
     *
     * @param storeJoin
     * @return
     */
    @Override
    public R applyStore(PcStoreJoin storeJoin) {
        PcStore pcStore = pcStoreMapper.selectByMid(storeJoin.getMid());
        if (pcStore == null || pcStore.getIsDelete() == 1) {
            PcStoreJoin pcStoreJoin = pcStoreJoinMapper.selectByMid(storeJoin.getMid());
            if (pcStoreJoin == null) {
                pcStoreJoinMapper.addStoreJoin(storeJoin);
            } else if (pcStoreJoin.getApplyType() == 0) {
                return R.fail("审核中");
            }
        } else {
            return R.fail("已有实体店 不能在申请！！！");
        }
        return R.ok("申请已提交");

    }

    /**
     * 审核
     *
     * @param examine
     * @return
     */
    @Transactional
    @Override
    public R examineStore(PcExamine examine) {
        //Long userId = BaseContextHandler.getUserId();
        PcStoreJoin pcStoreJoin = pcStoreJoinMapper.selectByMid(examine.getId());
        pcStoreJoin.setReason(examine.getReason());
        //pcStoreJoin.setApplyUser(userId);
        //审核失败
        if (examine.getApplyType() == 2) {
            pcStoreJoin.setApplyType(2);

        } else {
            pcStoreJoin.setApplyType(1);
            //创建店铺
            PcStore store = new PcStore();
            store.setStoreName(pcStoreJoin.getStoreName());
            store.setRemark(pcStoreJoin.getBrief());
            store.setStoreType(pcStoreJoin.getStoreType());
            store.setLongitude(pcStoreJoin.getLongitude());
            store.setLatitude(pcStoreJoin.getLatitude());
            store.setTelephone(pcStoreJoin.getTelephone());
            store.setBusinessHours(pcStoreJoin.getBusinessHours());
            store.setStorefrontImg(pcStoreJoin.getStorefrontImg());
            store.setBackgroundImg(pcStoreJoin.getBackgroundImg());
            store.setTurnover(new BigDecimal(0));
            store.setOrderQuantity(0);
            store.setProvince(pcStoreJoin.getRegisterProvince());
            store.setCity(pcStoreJoin.getRegisterCity());
            store.setArea(pcStoreJoin.getRegisterArea());
            store.setAddress(pcStoreJoin.getAddress());
            store.setMid(pcStoreJoin.getMid());
            store.setMobile(pcStoreJoin.getTelephone());
            pcStoreMapper.insertPcStore(store);

        }
        pcStoreJoinMapper.updateStoreJoin(pcStoreJoin);
        return R.ok();
    }

    /**
     * 修改店铺
     *
     * @param pcStore 店铺
     * @return 结果
     */
    @Override
    public R updatePcStore(PcStore pcStore) {
        pcStore.setUpdateTime( DateUtils.getNowDate());
        return pcStoreMapper.updatePcStore(pcStore) > 0 ? R.ok("修改成功") : R.fail("操作失败");
    }

//    /**
//     * 批量删除店铺
//     * @param ids 需要删除的店铺主键
//     * @return 结果
//     */
//    @Override
//    public R deletePcStoreByIds(Long[] ids) {
//        return pcStoreMapper.deletePcStoreByIds(ids) > 0 ? R.ok("批量删除成功") : R.fail("操作失败");
//    }

    /**
     * 删除店铺信息
     *
     * @param id 店铺主键
     * @return 结果
     */
    @Transactional
    @Override
    public R deletePcStoreById(Long id) {
        pcProductMapper.deletePcProductById(id);
        return pcStoreMapper.deletePcStoreById(id) > 0 ? R.ok("删除成功") : R.fail("操作失败");
    }
}
